/*
 *  Copyright (C) 2012 Компания ВИТ. web: http://diasled.ru Е-mail:  vit@diasled.ru
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ru.apertum.diasled.model.patients;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import javax.swing.table.AbstractTableModel;
import ru.apertum.diasled.model.Patient;

/**
 * Модель таблици пациентов.
 * @author Evgeniy Egorov, Aperum Projects
 */
public class PatientsTableModel extends AbstractTableModel {

    public PatientsTableModel() {
        patients = Patient.getPatients(null);
    }
    final private LinkedList<Patient> patients;
    
    public void filter(HashMap<String, Object> params){
        patients.clear();
        patients.addAll(Patient.getPatients(params));
    }

    @Override
    public int getRowCount() {
        return patients.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
                return patients.get(rowIndex).getId();
            case 1:
                return " " + patients.get(rowIndex).getSecondName();
            case 2:
                return " " + patients.get(rowIndex).getFirstName() + " " + patients.get(rowIndex).getMiddleName();
            case 3:
                return patients.get(rowIndex).getBirthday();
            case 4:
                return patients.get(rowIndex).getLastVisit();
            default:
                throw new AssertionError();
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "№";
            case 1:
                return "Фамилия";
            case 2:
                return "Имя, отчество";
            case 3:
                return "Дата рождения";
            case 4:
                return "Последний визит";
            default:
                throw new AssertionError();
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return Long.class;
            case 1:
                return String.class;
            case 2:
                return String.class;
            case 3:
                return Date.class;
            case 4:
                return Date.class;
            default:
                throw new AssertionError();
        }
    }

    public void add(Patient patient) {
        patients.add(patient);
    }

    /**
     * Поиск пациента по id
     * @param id
     * @return найденный пациент, null если не нашлось
     */
    public Patient getPatientById(Long id) {
        for (Patient patient : patients) {
            if (id.equals(patient.getId())) {
                return patient;
            }
        }
        return null;
    }
    
    public void removePatient(Patient patient){
        patients.remove(patient);
        Patient.removePatient(patient);
        fireTableDataChanged();
    }
}
